package tgc.edu.zz.controller;

import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.zz.service.PersionService;
import tgc.edu.zz.utils.AjaxResult;
import tgc.edu.zz.utils.TablePageable;
import tgc.edu.zz.vo.PersionVo;

@Controller
@RequestMapping("/persion")
public class PersionController {

	@Autowired
	private PersionService pService; 
	/**
	 * 
	 * @return 用户管理页面
	 */
	@RequestMapping("/page")
	public String page() {
		return "persion/page";
	}
	
	/**
	 * 
	 * @param form
	 * @param pageParam
	 * @return 展示所有用户列表（不展示管理员）
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Object list(PersionVo form,TablePageable pageParam) {
		PageRequest pageable = pageParam.bulidPageRequest();
		Specification<PersionVo> spec = buildSpec(form);
		Page<PersionVo> page= pService.findAll(spec, pageable);
		return AjaxResult.bulidPageResult(page);
		
	}
	/**
	 * 
	 * @param form
	 * @return 安全查询（是管理员不显示）
	 */
	private Specification<PersionVo> buildSpec(PersionVo form) {
		Specification<PersionVo> spec = new Specification<PersionVo>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<PersionVo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String identity = "用户";
				if (StringUtils.hasText(identity)) {
					rules.add(cb.like(root.get("identity"), "%"+identity+"%"));
				}
				
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	/**
	 * 编辑用户
	 * @param id
	 * @param map
	 * @return 编辑页面
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap map) {
		PersionVo model = new PersionVo();
		if(id!=null) {
			model=pService.findById(id);
		}
		map.put("model", model);
		return "persion/edit";
	}
	
	/**
	 * 更新用户列表
	 * @param model
	 * @return 用户信息页面
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(PersionVo model) {
		if(model.getId() == null) {
			PersionVo per = new PersionVo();
			per.setUsername(model.getUsername());
			per.setPassword(model.getPassword());
			per.setAddress(model.getAddress());
			per.setAge(model.getAge());
			per.setNation(model.getNation());
			per.setPhone(model.getPhone());
			per.setSex(model.getSex());
			per.setIdentity("用户");
			pService.save(per);
		}else {
			PersionVo model2 = pService.findById(model.getId());
			model2.setUsername(model.getUsername());
			model2.setPassword(model.getPassword());
			model2.setAddress(model.getAddress());
			model2.setAge(model.getAge());
			model2.setNation(model.getNation());
			model2.setPhone(model.getPhone());
			model2.setSex(model.getSex());
			model2.setIdentity("用户");
			pService.save(model2);
			
		}
		return "保存成功";
	}

	/**
	 * 删除用户
	 * @param id
	 * @return 用户信息页面
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String del(Integer id) {
		pService.deleteById(id);
		return "删除成功！";
	}
	
}
